package b.b.h;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build.VERSION;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnAttachStateChangeListener;
import android.view.View.OnHoverListener;
import android.view.View.OnLongClickListener;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.accessibility.AccessibilityManager;
import b.i.k.o;
import b.i.k.s;
import java.lang.reflect.Method;
import java.util.WeakHashMap;

/* compiled from: TooltipCompatHandler */
public class i0 implements OnLongClickListener, OnHoverListener, OnAttachStateChangeListener {
    public static i0 j;
    public static i0 k;
    public final View a;
    public final CharSequence b;
    public final int c;
    public final Runnable d = new a();
    public final Runnable e = new b();
    public int f;
    public int g;
    public j0 h;
    public boolean i;

    /* compiled from: TooltipCompatHandler */
    public class a implements Runnable {
        public void run() {
            i0.this.d(false);
        }
    }

    /* compiled from: TooltipCompatHandler */
    public class b implements Runnable {
        public void run() {
            i0.this.b();
        }
    }

    public i0(View view, CharSequence charSequence) {
        int scaledHoverSlop;
        this.a = view;
        this.b = charSequence;
        ViewConfiguration viewConfiguration = ViewConfiguration.get(view.getContext());
        Method method = s.a;
        if (VERSION.SDK_INT >= 28) {
            scaledHoverSlop = viewConfiguration.getScaledHoverSlop();
        } else {
            scaledHoverSlop = viewConfiguration.getScaledTouchSlop() / 2;
        }
        this.c = scaledHoverSlop;
        a();
        view.setOnLongClickListener(this);
        view.setOnHoverListener(this);
    }

    public static void c(i0 i0Var) {
        i0 i0Var2 = j;
        if (i0Var2 != null) {
            i0Var2.a.removeCallbacks(i0Var2.d);
        }
        j = i0Var;
        if (i0Var != null) {
            i0Var.a.postDelayed(i0Var.d, (long) ViewConfiguration.getLongPressTimeout());
        }
    }

    public final void a() {
        this.f = Integer.MAX_VALUE;
        this.g = Integer.MAX_VALUE;
    }

    public void b() {
        if (k == this) {
            k = null;
            j0 j0Var = this.h;
            if (j0Var != null) {
                j0Var.a();
                this.h = null;
                a();
                this.a.removeOnAttachStateChangeListener(this);
            } else {
                Log.e("TooltipCompatHandler", "sActiveHandler.mPopup == null");
            }
        }
        if (j == this) {
            c(null);
        }
        this.a.removeCallbacks(this.e);
    }

    public void d(boolean z) {
        View view = this.a;
        WeakHashMap weakHashMap = o.a;
        if (view.isAttachedToWindow()) {
            int i;
            long j;
            c(null);
            i0 i0Var = k;
            if (i0Var != null) {
                i0Var.b();
            }
            k = this;
            this.i = z;
            j0 j0Var = new j0(this.a.getContext());
            this.h = j0Var;
            View view2 = this.a;
            int i2 = this.f;
            int i3 = this.g;
            boolean z2 = this.i;
            CharSequence charSequence = this.b;
            if ((j0Var.b.getParent() != null ? 1 : null) != null) {
                j0Var.a();
            }
            j0Var.c.setText(charSequence);
            LayoutParams layoutParams = j0Var.d;
            layoutParams.token = view2.getApplicationWindowToken();
            int dimensionPixelOffset = j0Var.a.getResources().getDimensionPixelOffset(2131165528);
            if (view2.getWidth() < dimensionPixelOffset) {
                i2 = view2.getWidth() / 2;
            }
            if (view2.getHeight() >= dimensionPixelOffset) {
                dimensionPixelOffset = j0Var.a.getResources().getDimensionPixelOffset(2131165527);
                i = i3 + dimensionPixelOffset;
                i3 -= dimensionPixelOffset;
            } else {
                i = view2.getHeight();
                i3 = 0;
            }
            layoutParams.gravity = 49;
            dimensionPixelOffset = j0Var.a.getResources().getDimensionPixelOffset(z2 ? 2131165531 : 2131165530);
            View rootView = view2.getRootView();
            ViewGroup.LayoutParams layoutParams2 = rootView.getLayoutParams();
            if (!(layoutParams2 instanceof LayoutParams) || ((LayoutParams) layoutParams2).type != 2) {
                for (Context context = view2.getContext(); context instanceof ContextWrapper; context = ((ContextWrapper) context).getBaseContext()) {
                    if (context instanceof Activity) {
                        rootView = ((Activity) context).getWindow().getDecorView();
                        break;
                    }
                }
            }
            if (rootView == null) {
                Log.e("TooltipPopup", "Cannot find app view");
            } else {
                int identifier;
                rootView.getWindowVisibleDisplayFrame(j0Var.e);
                Rect rect = j0Var.e;
                if (rect.left < 0 && rect.top < 0) {
                    Resources resources = j0Var.a.getResources();
                    identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
                    identifier = identifier != 0 ? resources.getDimensionPixelSize(identifier) : 0;
                    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
                    j0Var.e.set(0, identifier, displayMetrics.widthPixels, displayMetrics.heightPixels);
                }
                rootView.getLocationOnScreen(j0Var.g);
                view2.getLocationOnScreen(j0Var.f);
                int[] iArr = j0Var.f;
                identifier = iArr[0];
                int[] iArr2 = j0Var.g;
                iArr[0] = identifier - iArr2[0];
                iArr[1] = iArr[1] - iArr2[1];
                layoutParams.x = (iArr[0] + i2) - (rootView.getWidth() / 2);
                int makeMeasureSpec = MeasureSpec.makeMeasureSpec(0, 0);
                j0Var.b.measure(makeMeasureSpec, makeMeasureSpec);
                makeMeasureSpec = j0Var.b.getMeasuredHeight();
                int[] iArr3 = j0Var.f;
                int i4 = ((iArr3[1] + i3) - dimensionPixelOffset) - makeMeasureSpec;
                i2 = (iArr3[1] + i) + dimensionPixelOffset;
                if (z2) {
                    if (i4 >= 0) {
                        layoutParams.y = i4;
                    } else {
                        layoutParams.y = i2;
                    }
                } else if (makeMeasureSpec + i2 <= j0Var.e.height()) {
                    layoutParams.y = i2;
                } else {
                    layoutParams.y = i4;
                }
            }
            ((WindowManager) j0Var.a.getSystemService("window")).addView(j0Var.b, j0Var.d);
            this.a.addOnAttachStateChangeListener(this);
            if (this.i) {
                j = 2500;
            } else {
                if ((this.a.getWindowSystemUiVisibility() & 1) == 1) {
                    j = 3000;
                    i2 = ViewConfiguration.getLongPressTimeout();
                } else {
                    j = 15000;
                    i2 = ViewConfiguration.getLongPressTimeout();
                }
                j -= (long) i2;
            }
            this.a.removeCallbacks(this.e);
            this.a.postDelayed(this.e, j);
        }
    }

    public boolean onHover(View view, MotionEvent motionEvent) {
        if (this.h != null && this.i) {
            return false;
        }
        AccessibilityManager accessibilityManager = (AccessibilityManager) this.a.getContext().getSystemService("accessibility");
        if (accessibilityManager.isEnabled() && accessibilityManager.isTouchExplorationEnabled()) {
            return false;
        }
        int action = motionEvent.getAction();
        if (action != 7) {
            if (action == 10) {
                a();
                b();
            }
        } else if (this.a.isEnabled() && this.h == null) {
            Object obj;
            action = (int) motionEvent.getX();
            int y = (int) motionEvent.getY();
            if (Math.abs(action - this.f) > this.c || Math.abs(y - this.g) > this.c) {
                this.f = action;
                this.g = y;
                obj = 1;
            } else {
                obj = null;
            }
            if (obj != null) {
                c(this);
            }
        }
        return false;
    }

    public boolean onLongClick(View view) {
        this.f = view.getWidth() / 2;
        this.g = view.getHeight() / 2;
        d(true);
        return true;
    }

    public void onViewAttachedToWindow(View view) {
    }

    public void onViewDetachedFromWindow(View view) {
        b();
    }
}
